﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace EvoGame.Bitmaps
{
    public class Image : bitmap
    {
        Texture2D tex;

        public Image(int w, int h)
            : base(w, h)
        {
            width = w;
            height = h;
            pixels = new UInt32[w * h];
            tex = new Texture2D(Game1.device, width, height);
        }

        public void setPixelScaled(uint x, uint y, uint color, int pixelW)
        {
            if (x > width) x = (uint)width;
            if (y > height) y = (uint)height;
            if (x < 0) x = 0;
            if (y < 0) y = 0;
            try
            {
                for (int yy = (int)y; yy < y + pixelW; yy++)
                {
                    for (int xx = (int)x; xx < x + pixelW; xx++)
                    {
                        if (x > width || y > height)
                        {
                            pixels[xx + yy * width - 1] = color;
                        } else
                        {
                            pixels[xx + yy * width] = color;
                        }
                    }
                }
            } catch { }
        }

        public void setPixelScaled(uint x, uint y, uint color, int pixelH, int pixelW)
        {
            if (x > width) x = (uint)width;
            if (y > height) y = (uint)height;
            if (x < 0) x = 0;
            if (y < 0) y = 0;
            try
            {
                for (int yy = (int)y; yy < y + pixelH; yy++)
                {
                    for (int xx = (int)x; xx < x + pixelW; xx++)
                    {
                        if (x > width || y > height)
                        {
                            pixels[xx + yy * width - 1] = color;
                        } else
                        {
                            pixels[xx + yy * width] = color;
                        }
                    }
                }
            } catch { }
        }

        public override void setPixel(uint x, uint y, uint color)
        {
            if (x > width) x = (uint)width;
            if (y > height) y = (uint)height;
            if (x < 0) x = 0;
            if (y < 0) y = 0;
            try
            {
                if (x > width || y > height)
                {
                    pixels[x + y * width - 1] = color;
                } else
                {
                    pixels[x + y * width] = color;
                }
            } catch { }
        }

        public override void update()
        {
            tex.SetData<UInt32>(pixels);
        }
    }
}
